/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package beans;

import dao.StaticsStrings;
import dao.impl.GrowMessages;
import dao.impl.RedimensionarDaoImpl;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import org.primefaces.context.RequestContext;
import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;

/**
 *
 * @author rafael
 */
@ManagedBean
@SessionScoped
public class BeanUpload  implements Serializable{

    private StreamedContent imagem = null;
    FileUploadEvent fileUploadEvent;

    public void doUpload(FileUploadEvent fileUploadEvent) {

        try {
            this.fileUploadEvent = fileUploadEvent;
            this.imagem = new DefaultStreamedContent(fileUploadEvent.getFile().getInputstream());
            RequestContext.getCurrentInstance().update("imagemCarro");
        } catch (IOException iOException) {
            GrowMessages.infoMessage(StaticsStrings.ERRO, iOException.getLocalizedMessage());
        }

    }
    
    public void inserirImagemInserirVeiculos() throws IOException {
        File imagemDefault = new RedimensionarDaoImpl().criarArquivoImagem(this.fileUploadEvent);
        File fileThumbs = new RedimensionarDaoImpl().criarArquivoThumbs(this.fileUploadEvent);
        new RedimensionarDaoImpl().criarArquivoTemporario(this.fileUploadEvent);
        new RedimensionarDaoImpl().redimensionarImagem(imagemDefault, imagemDefault);
        new RedimensionarDaoImpl().criarMiniaturas(fileThumbs, fileThumbs);
    }

    public FileUploadEvent getFileUploadEvent() {
        return fileUploadEvent;
    }

    public void setFileUploadEvent(FileUploadEvent fileUploadEvent) {
        this.fileUploadEvent = fileUploadEvent;
    }

    public StreamedContent getImagem() {
        return imagem;
    }

    public void setImagem(StreamedContent imagem) {
        this.imagem = imagem;
    }

}
